package com.lanou.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.mapper.ConfigFeeItemMapper;
import com.lanou.mapper.PaymentMeterMapper;
import com.lanou.pojo.ConfigFeeItemEntity;
import com.lanou.pojo.PaymentMeterEntity;
import com.lanou.service.PaymentMeterService;
import com.lanou.staticResource.FilePath;
import com.lanou.utils.JsonResult;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName PaymentMeterServiceImpl
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月27日
 * @Version: 1.0
 */
@Service
public class PaymentMeterServiceImpl implements PaymentMeterService {

    @Autowired
    private PaymentMeterMapper paymentMeterMapper;

    @Autowired
    private ConfigFeeItemMapper configFeeItemMapper;

    @Override
    public String getPaymentMeterList(String pageNum, String pageSize, String blockCode, String feeItemName) {
// 分页
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        List<PaymentMeterEntity> paymentMeterList = paymentMeterMapper.getPaymentMeterList(blockCode, feeItemName);

        PageInfo<PaymentMeterEntity> pageInfo = new PageInfo<>(paymentMeterList);
        JsonResult<PageInfo<PaymentMeterEntity>> jsonResult = new JsonResult<>();
        jsonResult.setData(pageInfo);
        jsonResult.setMessage("请求成功");
        jsonResult.setStatus(200);

        return JSON.toJSONString(jsonResult);
    }

    @Override
    public String uploadFile(MultipartFile multipartFile) throws IOException {
        String uuidFileName = UUID.randomUUID().toString().replace("-", "");
        String realPath = FilePath.UPLOAD_FILE_PATH + "/" + uuidFileName + ".xlsx";
        File file = new File(realPath);
        multipartFile.transferTo(file);
        return realPath;
    }

    @Override
    public String analyseFileToDb(String filePath) throws Exception {
        File file = new File(filePath);
        InputStream inputStream = Files.newInputStream(file.toPath());
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);

        int lastRowNum = sheetAt.getLastRowNum();

        List<PaymentMeterEntity> paymentMeterEntityList = new ArrayList<>();
        Map<Integer, List<String>> map = new HashMap<>();

        for (int i = 2; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            List<String> rowList = new ArrayList<>();
            for (Cell cell : row) {
                String stringCellValue = cell.getStringCellValue();
                rowList.add(stringCellValue);
            }
            map.put(i - 2, rowList);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        for (int i = 0; i < map.size(); i++) {
            List<String> stringList = map.get(i);

            System.out.println(stringList + "输出字符串列表");

            PaymentMeterEntity paymentMeterEntity = new PaymentMeterEntity();

            String uuidStr = UUID.randomUUID().toString().replace("-", "");
            paymentMeterEntity.setId(uuidStr); // 设置id
            paymentMeterEntity.setBlockCode(stringList.get(0));// 设置商业区编号
            paymentMeterEntity.setResourceId("");// 收费的资源
            paymentMeterEntity.setResourceType("house"); // 资源类型
            paymentMeterEntity.setResourceName(""); // 资源名

            List<ConfigFeeItemEntity> configFeeItemEntities = configFeeItemMapper.selectConfigFeeItemList("house", stringList.get(2));
            paymentMeterEntity.setFeeItemId(configFeeItemEntities.get(0).getId()); // 收费项id

            paymentMeterEntity.setFeeItemName(stringList.get(2));// 收费项名
            paymentMeterEntity.setCurrentIndexDate(simpleDateFormat.parse(stringList.get(6)));// 当前读表时间
            paymentMeterEntity.setCurrentIndex(stringList.get(5));// 当前读数
            paymentMeterEntity.setLastIndex(stringList.get(3));// 上期读数
            paymentMeterEntity.setLastIndexDate(simpleDateFormat.parse(stringList.get(4)));// 上期读表时间
            paymentMeterEntity.setMultiple(stringList.get(7));// 倍率
            paymentMeterEntity.setLoss(stringList.get(8));// 损耗

            // （本期读数 - 上期读数） * 倍率 + 损耗
            int result = (Integer.valueOf(stringList.get(5)) - Integer.valueOf(stringList.get(3))) * Integer.valueOf(stringList.get(7));
            paymentMeterEntity.setResult(String.valueOf(result));// 结果
            paymentMeterEntity.setEnabledMark(1);

            paymentMeterEntityList.add(paymentMeterEntity);
        }

        for (PaymentMeterEntity paymentMeter : paymentMeterEntityList) {
            paymentMeterMapper.insertPaymentMeterListByExcel(paymentMeter);
        }

        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setMessage("添加成功");
        jsonResult.setStatus(200);

        file.delete();

        inputStream.close();
        return JSON.toJSONString(jsonResult);
    }

    // 根据id删除数据
    @Override
    public void deletePaymentMeterById(String id) {
        paymentMeterMapper.deletePaymentMeterById(id);
    }

    // 根据修改数据
    @Override
    public void modifyPaymentMeter(PaymentMeterEntity paymentMeterEntity) {
        paymentMeterMapper.modifyPaymentMeter(paymentMeterEntity);
    }
}